package com.gzw.shop.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.gzw.shop.R;

/**
 * Created by JACKYGUO on 2017-03-02.
 * <p>
 * 自定义toolbar
 */

public class GToolbar extends Toolbar {

    private LayoutInflater mInflater;
    private View view;

    private TextView mTitle;
    private EditText searchView;
    private Button rightBtn;

    private ImageView ivLeft;
    private ImageView ivRight;


    public GToolbar(Context context) {
        this(context, null);
    }

    public GToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();
        setContentInsetsRelative(10, 10);

        if (attrs != null) {
            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
                    R.styleable.GToolbar, defStyleAttr, 0);

            final Drawable rightIcon = a.getDrawable(R.styleable.GToolbar_rightButton);
            if (rightIcon != null) {
                setRightButtonIcon(rightIcon);
            }

            String rightButtonText = a.getString(R.styleable.GToolbar_rightButtonText);

            setRightBtnText(rightButtonText);
            showRightBtn();


            boolean isShowSearchView = a.getBoolean(R.styleable.GToolbar_isShowSearchView, false);

            if (isShowSearchView) {
                showSearch();
                hideTitle();
            }

            boolean isShowIvLeft = a.getBoolean(R.styleable.GToolbar_isShowlvLeft, false);

            if (isShowIvLeft) {
                showLeftIv();
            }

            a.recycle();
        }
    }

    private void initView() {


        mInflater = LayoutInflater.from(getContext());

        if (view == null) {

            view = mInflater.inflate(R.layout.toolbar, null);

            mTitle = (TextView) view.findViewById(R.id.toolbar_title);
            searchView = (EditText) view.findViewById(R.id.toolbar_searchview);
            rightBtn = (Button) view.findViewById(R.id.toolbar_rightButton);
            ivLeft = (ImageView) view.findViewById(R.id.iv_left);
            ivRight = (ImageView) view.findViewById(R.id.iv_right);
            LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
            addView(view, lp);
        }
    }

    public void setRightBtnOnClickListener(OnClickListener li) {
        rightBtn.setOnClickListener(li);
    }


    @Override
    public void setTitle(@StringRes int resId) {
        setTitle(getContext().getText(resId));
    }

    @Override
    public void setTitle(CharSequence title) {
        initView();
        if (mTitle != null) {
            mTitle.setText(title);
            showTitle();
        }
    }

    public void showTitle() {
        if (mTitle != null)
            mTitle.setVisibility(VISIBLE);
    }

    public void hideTitle() {
        if (mTitle != null)
            mTitle.setVisibility(GONE);
    }

    public void showSearch() {
        if (searchView != null)
            searchView.setVisibility(VISIBLE);
    }

    public void hideSearch() {
        if (searchView != null)
            searchView.setVisibility(GONE);
    }

    public void showRightBtn() {
        if (rightBtn != null)
            rightBtn.setVisibility(VISIBLE);
    }

    public void showLeftIv() {
        if (ivLeft != null) {
            ivLeft.setVisibility(VISIBLE);
        }
    }

    public void setIvLeftOnclickListener(OnClickListener listener) {
        ivLeft.setOnClickListener(listener);
    }

    public void hideRightBtn() {
        if (rightBtn != null)
            rightBtn.setVisibility(GONE);
    }

    public void setRightButtonIcon(Drawable icon) {
        if (rightBtn != null) {
            rightBtn.setBackground(icon);
            showRightBtn();
        }
    }

    public void setRightButtonIcon(int icon) {
        if (rightBtn != null) {
            rightBtn.setBackground(getResources().getDrawable(icon));
            showRightBtn();
        }
    }

    public void setIvRightIcon(int icon) {
        if (ivRight != null) {
            rightBtn.setVisibility(GONE);
            ivRight.setVisibility(VISIBLE);
            ivRight.setBackground(getResources().getDrawable(icon));
        }
    }

    public void setIvRightTag(Object object) {
        if (ivRight != null) {
            ivRight.setTag(object);
        }
    }

    public Object getIvRightTag() {
        if (ivRight != null) {
            return ivRight.getTag();
        }
        return null;
    }

    public void setIvRightOnClickListener(OnClickListener listener) {
        ivRight.setOnClickListener(listener);
    }


    public void setRightBtnText(String s) {
        if (rightBtn != null) {
            rightBtn.setText(s);
        }
    }

    public void setRightBtnTag(Object object) {
        if (rightBtn != null) {
            rightBtn.setTag(object);
        }
    }

    public Object getRightBtnTag() {
        if (rightBtn != null) {
            return rightBtn.getTag();
        }
        return null;
    }
}
